package org.jarraiders.musiclibrary.server.service;

import org.jarraiders.musiclibrary.domain.Album;
import org.jarraiders.musiclibrary.domain.Artist;
import org.jarraiders.musiclibrary.domain.Track;
import org.jarraiders.musiclibrary.shared.domain.AlbumDTO;
import org.jarraiders.musiclibrary.shared.domain.ArtistDTO;
import org.jarraiders.musiclibrary.shared.domain.TrackDTO;

/**
 * @author AlteroStudio
 */
public class ConverterFabric {
	
	private static final ArtistServiceDAO artistService = new ArtistServiceDAO();
	private static final AlbumServiceDAO albumService = new AlbumServiceDAO();
	private static final TrackServiceDAO trackService = new TrackServiceDAO();

	public static ArtistDTO getArtistDTO(Artist artist) {
		ArtistDTO artistDTO=new ArtistDTO();
		artistDTO.setArtistName(artist.getArtistName());
		artistDTO.setArtistId(artist.getArtistId().toString());
		return artistDTO;
	}

	public static Artist getArtistDAO(ArtistDTO artistDTO) {
		Artist artist = artistService.getEntityById(artistDTO.getArtistId());		
		artist.setArtistName(artistDTO.getArtistName());		
		return artist;
	}

	public static AlbumDTO getAlbumDTO(Album album) {
		AlbumDTO albumDTO=new AlbumDTO();
		albumDTO.setAlbumName(album.getAlbumName());
		albumDTO.setAlbumYear(album.getAlbumYear());
		albumDTO.setAlbumId(album.getAlbumId().toString());
		albumDTO.setReferencedArtistId(album.getReferencedArtist().getArtistId().toString());
		return albumDTO;
	}
	
	public static Album getAlbumDAO(AlbumDTO albumDTO) {
		Album album = albumService.getEntityById(albumDTO.getAlbumId());
		
		album.setAlbumName(albumDTO.getAlbumName());
		album.setAlbumYear(albumDTO.getAlbumYear());
		
		String oldReferencedArtistID = album.getReferencedArtist().getArtistId().toString();
		String newReferencedArtistID = albumDTO.getReferencedArtistId();
		if (!oldReferencedArtistID.equals(newReferencedArtistID)) {
			Artist artist = artistService.getEntityById(albumDTO.getReferencedArtistId());
			album.setReferencedArtist(artist);
		}
		
		return album;
	}

	public static TrackDTO getTrackDTO(Track track) {
		TrackDTO trackDTO= new TrackDTO();
		trackDTO.setTrackTitle(track.getTrackTitle());
		trackDTO.setTrackNumber(track.getTrackNumber());
		trackDTO.setTrackId(track.getTrackId().toString());
		trackDTO.setReferencedAlbumId(track.getReferencedAlbum().getAlbumId().toString());
		return trackDTO;
	}
	
	public static Track getTrackDAO(TrackDTO trackDTO) {
		Track track = trackService.getEntityById(trackDTO.getTrackId());
		
		track.setTrackNumber(trackDTO.getTrackNumber());
		track.setTrackTitle(trackDTO.getTrackTitle());
		
		String oldReferencedAlbumID = track.getReferencedAlbum().getAlbumId().toString();
		String newReferencedAlbumID = trackDTO.getReferencedAlbumId();
		if (!oldReferencedAlbumID.equals(newReferencedAlbumID)) {
			Album album = albumService.getEntityById(trackDTO.getReferencedAlbumId());
			track.setReferencedAlbum(album);
		}
		
		return track;
	}

}
